/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Academic;
import java.sql.*;
/**
 *
 * @author mishra
 */
public class Query {
    static Connection con=null;
    
    public static int add(AcademicBean ab) throws SQLException
    { int i=0,k=0;
        try{
            con=DBConnection.Connection.getConnection();
            PreparedStatement pre=con.prepareStatement("insert into academic values(academicid.nextval,?,?,?,?,?,?,?,?,?,?,?,?,?,sysdate)");
            pre.setInt(1, ab.getAddissionId());
            pre.setString(2, ab.getHighSchool());
            pre.setString(3, ab.getHighBoardName());
            pre.setInt(4, ab.getHighMaxMarks());
            pre.setInt(5, ab.getHighMarksObtained());
            pre.setString(6, ab.getInterSchool());
            pre.setString(7, ab.getInterBoardName());
            pre.setInt(8, ab.getInterMaxMarks());
            pre.setInt(9, ab.getInterMarksObtained());
            pre.setString(10, ab.getOther());
            pre.setString(11, ab.getOtherBoardName());
            pre.setInt(12, ab.getOtherMaxMarks());
            pre.setInt(13, ab.getOhterMarksObtained());
            i=pre.executeUpdate();
            if(i!=0){
                k=getMaxId();
            }
        }catch(Exception e)
        {
            System.out.println(e);
        }finally{con.close();}
        return k;
    }
    public static int getMaxId() throws SQLException
    {
        int i=0;
        try{
            con=DBConnection.Connection.getConnection();
            PreparedStatement pre=con.prepareStatement("select max(academic_id) from academic");
            ResultSet rs=pre.executeQuery();
            if(rs.next())
                i=rs.getInt(1);
        }catch(Exception e)
        {return i;}
        finally{con.close();}
        return i;
    }
public static int edit(AcademicBean ab) throws SQLException
    { int i=0;
        try{
            con=DBConnection.Connection.getConnection();
            PreparedStatement pre=con.prepareStatement("update academic set ADMISSION_ID=?,HIGH_SCHOOL=?,HIGH_BORAD_NAME=?,HIGH_MAXIMUM_MARKS=?,HIGH_MARKS_OBTAINED=?,INTER_SCHOOL=?,INTER_BOARD_NAME=?,INTER_MAXIMUM_MARKS=?,INTER_MARKS_OBTAINED=?,OTHER=?,OTHER_BOARD_NAME=?,OTHER_MAXIMUM_MARKS=?,OTHER_MARKS_OBTAINED=?,ENTRY_DATE=sysdate where ACADEMIC_ID=?");
            pre.setInt(1, ab.getAddissionId());
            pre.setString(2, ab.getHighSchool());
            pre.setString(3, ab.getHighBoardName());
            pre.setInt(4, ab.getHighMaxMarks());
            pre.setInt(5, ab.getHighMarksObtained());
            pre.setString(6, ab.getInterSchool());
            pre.setString(7, ab.getInterBoardName());
            pre.setInt(8, ab.getInterMaxMarks());
            pre.setInt(9, ab.getInterMarksObtained());
            pre.setString(10, ab.getOther());
            pre.setString(11, ab.getOtherBoardName());
            pre.setInt(12, ab.getOtherMaxMarks());
            pre.setInt(13, ab.getOhterMarksObtained());
            pre.setInt(14, ab.getAcademicId());
            i=pre.executeUpdate();
            
        }catch(Exception e)
        {
            System.out.println(e);
        }finally{con.close();}
        return i;
    }    

 public static int chkAcademic(int admissionId, int academicId) throws SQLException {
        int i = 0;
        try {
            con = DBConnection.Connection.getConnection();
            PreparedStatement pre = con.prepareStatement("select * from academic where ADMISSION_ID=? or academic_id=?");
            pre.setInt(1, admissionId);
            pre.setInt(2, academicId);
            ResultSet rs = pre.executeQuery();
            if (rs.next()) {
                i = 1;
            }
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            con.close();
        }
        return i;
    }
}
